package agentV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/pkg/file"
	"hc_server/routers/api"
	"hc_server/routers/service/deviceService"
	"hc_server/runtime/e"
)

type DeviceModelInfoAddForm struct {
	No     string `json:"no"`     //设备信息
	Type   string `json:"type"`   //设备类型
	TypeId string `json:"typeId"` //设备类型id
}

// DeviceModelInfoAdd 设备类型信息添加
func DeviceModelInfoAdd(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceModelInfoAddForm
		errCode int
	)

	_, errCode = app.BindAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	iconImg, err := api.VerifyFileAndSave(c, "icon", file.GetRandomFileName(), "device/")
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	deviceService := deviceService.Model{
		No:     param.No,
		Type:   param.Type,
		Icon:   iconImg,
		TypeId: cast.ToInt(param.TypeId),
	}

	err = deviceService.DeviceModelInfoAdd()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", nil)
}

type DeviceInfoAddParam struct {
	DeviceSn      string      `json:"device_sn"`
	DeviceModelId interface{} `json:"device_model_id"` //设备类型
	DeviceTUSN    string      `json:"device_tusn"`     //设备型号
}

// DeviceInfoAdd 添加设备信息
func DeviceInfoAdd(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceInfoAddParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	deviceService := deviceService.Device{
		DeviceSn:      param.DeviceSn,
		DeviceModelId: cast.ToInt(param.DeviceModelId),
		DeviceTUSN:    param.DeviceTUSN,
		AgentId:       cast.ToInt(userId),
	}

	err := deviceService.DeviceInfoAdd()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", nil)
}

type DeviceModelInfoListParam struct {
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
}

// DeviceModelInfoList 设备型号列表
func DeviceModelInfoList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceModelInfoListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	deviceService := deviceService.Model{
		AgentId:  cast.ToInt(userId),
		PageSize: cast.ToInt(param.PageSize),
		PageNum:  cast.ToInt(param.PageNum),
	}

	result, err := deviceService.DeviceModelInfoList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type DeviceInfoSearchParam struct {
	PageNum  any    `json:"pageNum"`
	PageSize any    `json:"pageSize"`
	Keyword  string `json:"keyword,omitempty"`
}

// DeviceInfoSearch 设备型号搜索
func DeviceInfoSearch(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceInfoSearchParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	deviceService := deviceService.Device{
		AgentId:  cast.ToInt(userId),
		PageSize: cast.ToInt(param.PageSize),
		PageNum:  cast.ToInt(param.PageNum),
		Keyword:  param.Keyword,
	}

	result, err := deviceService.DeviceInfoSearch()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type DeviceInfoListParam struct {
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
}

// DeviceInfoList 设备列表
func DeviceInfoList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceInfoListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	deviceService := deviceService.Device{
		AgentId:  cast.ToInt(userId),
		PageSize: cast.ToInt(param.PageSize),
		PageNum:  cast.ToInt(param.PageNum),
	}

	result, err := deviceService.DeviceInfoList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type DeviceInfoParam struct {
	Id any `json:"id"`
}

func DeviceInfo(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceInfoParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	deviceService := deviceService.Device{
		Id: cast.ToInt(param.Id),
	}

	result, err := deviceService.DeviceInfo()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type DeviceAllocateInfoListParam struct {
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
	Id       any `json:"id"`
}

// DeviceAllocateInfoList 获取可划拨设备列表
func DeviceAllocateInfoList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceAllocateInfoListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	deviceService := deviceService.Device{
		AgentId:       cast.ToInt(userId),
		PageSize:      cast.ToInt(param.PageSize),
		PageNum:       cast.ToInt(param.PageNum),
		DeviceModelId: cast.ToInt(param.Id),
	}

	result, err := deviceService.DeviceAllocateInfoList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type DeviceRecallInfoListParam struct {
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
	Id       any `json:"id"`
	AgentId  any `json:"agentId"`
}

// DeviceRecallInfoList 获取可召回设备列表
func DeviceRecallInfoList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceRecallInfoListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	deviceService := deviceService.Device{
		AgentId:       cast.ToInt(param.AgentId),
		PageSize:      cast.ToInt(param.PageSize),
		PageNum:       cast.ToInt(param.PageNum),
		DeviceModelId: cast.ToInt(param.Id),
	}

	result, err := deviceService.DeviceRecallInfoList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type DeviceRecallAgentListParam struct {
	PageNum  any `json:"pageNum"`
	PageSize any `json:"pageSize"`
	Id       any `json:"id"`
}

func DeviceRecallAgentList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceRecallAgentListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	deviceService := deviceService.Device{
		AgentId:       cast.ToInt(userId),
		PageSize:      cast.ToInt(param.PageSize),
		PageNum:       cast.ToInt(param.PageNum),
		DeviceModelId: cast.ToInt(param.Id),
	}

	result, err := deviceService.DeviceRecallAgentList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type DeviceInfoListSearchParam struct {
	Id            any    `json:"id"`
	PageNum       any    `json:"pageNum"`
	PageSize      any    `json:"pageSize"`
	SnFrom        string `json:"snFrom,omitempty"`        //设备sn 开始区间
	SnTo          string `json:"snTo,omitempty"`          //设备sn 结束区间
	CodeFrom      string `json:"codeFrom,omitempty"`      //收款码 开始区间
	CodeTo        string `json:"codeTo,omitempty"`        //收款码 结束区间
	StockStatus   any    `json:"stockStatus,omitempty"`   //库存状态：0=待出库 1=已出库 全部=不传或者空字符串
	BindingStatus any    `json:"bindingStatus,omitempty"` //绑定状态：0=未绑定 1=已绑定 全部=不传或者空字符串
	StockTime     string `json:"stockTime,omitempty"`     //入库时间
	BindingTime   string `json:"bindingTime,omitempty"`   //绑定时间
	AgentId       any    `json:"agentId,omitempty"`       //直属下级
	MerchantId    any    `json:"merchantId,omitempty"`    //所属商户
}

func DeviceInfoListSearch(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   DeviceInfoListSearchParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	if param.AgentId == nil {
		param.AgentId, _ = c.Get("userId")
	}
	stockTime := cast.ToTime(param.StockTime)
	bindingTime := cast.ToTime(param.BindingTime)

	deviceService := deviceService.Device{
		SnFrom:        param.SnFrom,
		SnTo:          param.SnTo,
		CodeFrom:      param.CodeFrom,
		CodeTo:        param.CodeTo,
		StockStatus:   cast.ToString(param.StockStatus),
		BindingStatus: cast.ToString(param.BindingStatus),
		StockTime:     &stockTime,
		BindingTime:   &bindingTime,
		AgentId:       cast.ToInt(param.AgentId),
		MerchantId:    cast.ToInt(param.MerchantId),
		PageSize:      cast.ToInt(param.PageSize),
		PageNum:       cast.ToInt(param.PageNum),
		DeviceModelId: cast.ToInt(param.Id),
	}

	result, err := deviceService.DeviceInfoListSearch()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}
